package site.qjdb.service.impl;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import site.qjdb.dao.NoticeDao;
import site.qjdb.pojo.*;
import site.qjdb.service.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {
	@Resource
	private NoticeDao noticeDao;
	@Resource
	private CommentService commentService;
	@Resource
	private GoodsService goodsService;
	@Resource 
	private ReplyService replyService;
	@Resource
	private UserService userService;
	@Resource
	private WantService wantService;

	@Override
	public void addNotice(Notice notice) {
		noticeDao.addNotice(notice);
	}

	@Override
	public void deleteNotice(Notice notice) {
		Map<String, Object> map = new HashMap<>();
		notice.setIsDel(0);
		map.put("notice", notice);
		List<Notice> noticeList = noticeDao.selectNoticeAll(map);
		if(noticeList != null && noticeList.size() > 0) {
			notice.setIsDel(1);
			noticeDao.updateNotice(notice);
		}
	}
	
	@Override
	public List<Notice> findNoticeAll(Integer uid) {
		Map<String, Object> map = new HashMap<>();
		Notice notice = new Notice();
		notice.setUid(uid);
		// 未删除
		notice.setIsDel(0);
		// 新通知
		notice.setIsNew(1);
		map.put("notice", notice);
		List<Notice> noticeList = noticeDao.selectNoticeAll(map);
		if(noticeList == null || noticeList.size() == 0) {
			return null;
		} else {
			return noticeList;
		}
	}

	@Override
	public List<Notice> findNoticeByType(Integer uid, Integer type) {
		Map<String, Object> map = new HashMap<>();
		Notice notice = new Notice();
		notice.setUid(uid);
		// 未删除
		notice.setIsDel(0);
		// 设置类型
		notice.setType(type);
		map.put("notice", notice);
		// 排序规则：新通知，时间
		map.put("orderBy", "notice.isNew desc,notice.time desc");
		List<Notice> noticeList = noticeDao.selectNoticeAll(map);
		if(noticeList == null || noticeList.size() == 0) {
			return null;
		}
		// 添加实体类
		for(Notice _notice : noticeList) {
			Integer _type = _notice.getType();
			NoticeItem noticeItem = new NoticeItem();
			// 回复我的商品
			if(_type == 10) {
				Integer commentId = _notice.getCommentId();
				// 设置留言
				Comment comment = new Comment();
				comment.setCommentId(commentId);
				comment.setIsDel(0);
				Comment newComment = commentService.findComment(comment);
				if(newComment != null) {
					Goods goods = new Goods();
					goods.setGid(newComment.getGid());
					Goods newGoods = goodsService.findGoods(goods);
					if(newGoods != null) {
						// 设置noticeItem
						noticeItem.setContent(newComment.getContent());
						noticeItem.setImgPath(newComment.getUser().getImgPath());
						noticeItem.setItemName(newGoods.getName());
						noticeItem.setNickname(newComment.getUser().getNickname());
						noticeItem.setTime(_notice.getTime());
						noticeItem.setTitle("回复了我的商品");
						noticeItem.setUrl("/qb/page/user/search/desc.html?" + newGoods.getGid());
						_notice.setNoticeItem(noticeItem);
					}
				}
			}
			// 回复我的留言
			if(_type == 11) {
				Integer repyId = _notice.getRid();
				// 设置回复
				Reply reply = new Reply();
				reply.setRid(repyId);
				Reply newReply = replyService.findReply(reply);
				if(newReply != null) {
					Comment comment = new Comment();
					comment.setCommentId(newReply.getSuperiorId());
					comment.setIsDel(0);
					Comment newComment = commentService.findComment(comment);
					if(newComment != null) {
						Goods goods = new Goods();
						goods.setGid(newComment.getGid());
						Goods newGoods = goodsService.findGoods(goods);
						if(newGoods != null) {
							// 设置noticeItem
							noticeItem.setContent(newReply.getContent());
							noticeItem.setImgPath(newReply.getUser().getImgPath());
							noticeItem.setItemName(newComment.getContent());
							noticeItem.setNickname(newReply.getUser().getNickname());
							noticeItem.setTime(_notice.getTime());
							noticeItem.setTitle("回复了我的留言");
							noticeItem.setUrl("/qb/page/user/search/desc.html?" + newGoods.getGid());
							_notice.setNoticeItem(noticeItem);
						}
					}
				}
			}
			// 回复了我的求购信息
			if(_type == 12) {
				Integer repyId = _notice.getRid();
				// 设置回复
				Reply reply = new Reply();
				reply.setRid(repyId);
				Reply newReply = replyService.findReply(reply);
				if(newReply != null) {
					// 设置求购信息
					Want want = new Want();
					want.setWid(newReply.getSuperiorId());
					want.setIsDel(0);
					Want newWant = wantService.findWant(want);
					if(newWant != null) {
						// 设置noticeItem
						noticeItem.setContent(newReply.getContent());
						noticeItem.setImgPath(newReply.getUser().getImgPath());
						noticeItem.setItemName(newWant.getContent());
						noticeItem.setNickname(newReply.getUser().getNickname());
						noticeItem.setTime(_notice.getTime());
						noticeItem.setTitle("回复了我的求购信息");
						noticeItem.setUrl("/qb/page/user/buy/want.html?wd=" + newWant.getContent());
						_notice.setNoticeItem(noticeItem);
					}
				}
			}
			// 赞了我的留言
			if(_type == 20) {
				Integer commentId = _notice.getCommentId();
				// 设置留言
				Comment comment = new Comment();
				comment.setIsDel(0);
				comment.setCommentId(commentId);
				Comment newComment = commentService.findComment(comment);
				if(newComment != null) {
					Goods goods = new Goods();
					goods.setGid(newComment.getGid());
					Goods newGoods = goodsService.findGoods(goods);
					if(newGoods != null) {
						User newUser = userService.findUser(_notice.getOtherUid());
						if(newUser != null) {
							// 设置noticeItem
							noticeItem.setImgPath(newUser.getImgPath());
							noticeItem.setNickname(newUser.getNickname());
							noticeItem.setItemName(newComment.getContent());
							noticeItem.setTime(_notice.getTime());
							noticeItem.setTitle("赞了我的留言");
							noticeItem.setUrl("/qb/page/user/search/desc.html?" + newGoods.getGid());
							_notice.setNoticeItem(noticeItem);
						}
					}
				}
			}
			// 赞了我的求购信息
			if(_type == 21) {
				Integer wid = _notice.getWid();
				// 设置留言
				Want want = new Want();
				want.setIsDel(0);
				want.setWid(wid);
				Want newWant = wantService.findWant(want);
				if(newWant != null) {
					User newUser = userService.findUser(_notice.getOtherUid());
					if(newUser != null) {
						// 设置noticeItem
						noticeItem.setImgPath(newUser.getImgPath());
						noticeItem.setNickname(newUser.getNickname());
						noticeItem.setItemName(newWant.getContent());
						noticeItem.setTime(_notice.getTime());
						noticeItem.setTitle("赞了我的求购信息");
						noticeItem.setUrl("/qb/page/user/buy/want.html?wd=" + newWant.getContent());
						_notice.setNoticeItem(noticeItem);
					}
				}
				
			}
		}
		return noticeList;
	}

	@Override
	public void clearNew(Integer uid, Integer type) {
		Notice notice = new Notice();
		notice.setUid(uid);
		notice.setIsNew(0);
		notice.setType(type);
		noticeDao.updateNotice(notice);
	}

}
